package in.abacritt.android.xclock;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

public class OpenWeatherUpdateService extends IntentService {

	public OpenWeatherUpdateService() {
		super("WeatherUpdateService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		//Keep the wake lock till we finish updating weather
		PowerManager mgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
		WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
				"OpenWeatherUpdaterWakeLock");
		wakeLock.acquire();

		OpenWeatherClient openWeatherClient = new OpenWeatherClient(this);
		WeatherInfo currentWeatherInfo = openWeatherClient.getCurrentWeather();

		// Broadcast the weather
		Intent broadcastIntent = new Intent();
		broadcastIntent
				.setAction(XClockWidgetProvider.MY_WIDGET_WEATHER_UPDATE_COMP);
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		broadcastIntent.putExtra(XClockWidgetProvider.MY_WEATHER_INFO,
				currentWeatherInfo);
		sendBroadcast(broadcastIntent);

		wakeLock.release();
	}

}
